﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConverterInterface;

namespace DelphiConverterLibrary
{
    public class NumberState : State
    {
        private NumberState() {  }

        private static NumberState instance = new NumberState();

        public static NumberState Instance
        {
            get {return instance;}
        }

        private char c;
        private bool point;

        public override void Enter(DelphiConverter owner)
        {
            point = false;
        }

        public override void Execute(DelphiConverter owner)
        {
            if (owner.AvailableChars() <= 0)
            {
                owner.machine.Finished = true;
                owner.machine.ChangeState(NothingState.Instance);
            }
            else
            {
                c = owner.Peek();
                if (c == '\r')
                {
                    owner.machine.ChangeState(NothingState.Instance);
                }
                else
                {
                    owner.Forward();
                    
                    if (c == '.' && !point)
                    {
                        point = true;
                    }

                    else if (char.IsDigit(c))
                    {
                        owner.machine.Buffer.Append(c);
                    }

                    else
                    {
                        owner.machine.ChangeState(NothingState.Instance);
                    }

                    
                }

            }
        }

        public override void Exit(DelphiConverter owner)
        {
            owner.machine.Buffer.Insert(0, "<span class=\'number\'>");
            owner.machine.Buffer.Append("</span>");
            owner.FlushBuffer();
            owner.machine.Buffer.Append(c);
        }

    }
}
